<template>
  <ele-modal
    form
    width="95%"
    :model-value="modelValue"
    title="资金明细"
    @update:modelValue="updateModelValue"
  >
    <ele-pro-table
      ref="tableRef"
      row-key="userId"
      :columns="columns"
      :datasource="datasource"
      :show-overflow-tooltip="true"
      highlight-current-row
      border
      stripe
    >
      <template #action="{ row }">
        <el-button type="danger" size="small" @click="del(row)">
          删除
        </el-button>
      </template>
    </ele-pro-table>
  </ele-modal>
</template>

<script setup>
  import { ref } from 'vue';
  import {
    pageTenantBalanceDetail,
    delTenantBalanceDetail
  } from '@/api/company';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessageBox } from 'element-plus/es';
  const emit = defineEmits(['done', 'update:modelValue']);
  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,
    /** 修改回显的数据 */
    data: Object,
    tenantId: Number
  });

  /** 表格实例 */
  const tableRef = ref(null);

  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };
  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'id',
      label: 'ID',
      width: 100
    },
    {
      prop: 'cate',
      label: '分类',
      width: 100
    },
    {
      prop: 'create_time',
      label: '时间',
      width: 160
    },
    {
      prop: 'amount',
      label: '金额',
      width: 100,
      slot: 'amount'
    },
    {
      prop: 'balance',
      label: '变动后余额',
      width: 100
    },
    {
      label: '备注',
      prop: 'note'
    },
    {
      slot: 'action',
      label: '操作',
      width: 100
    }
  ]);
  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageTenantBalanceDetail({
      ...where,
      ...orders,
      page,
      limit,
      tenant_id: props.tenantId
    });
  };
  /** 删除 */
  const del = (row) => {
    ElMessageBox.confirm('删除此记录', '系统提示', {
      type: 'warning',
      draggable: true
    })
      .then(() => {
        const loading = EleMessage.loading({
          message: '正在处理中..',
          mask: true
        });
        delTenantBalanceDetail({ id: row.id })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功!');
            reload();
          })
          .catch(() => {
            loading.close();
          });
      })
      .catch(() => {});
  };
  const reload = (where) => {
    tableRef.value?.reload?.({ page: 1, where });
  };
</script>

<style lang="scss" scoped>
  .detail-table :deep(.el-descriptions__label) {
    width: 108px;
    text-align: right;
    font-weight: normal;
  }
</style>
